package com.yun.zhong.fu.usercenter.service;

import com.yun.zhong.fu.usercenter.dao.BonusEventLogDao;
import com.yun.zhong.fu.usercenter.dao.UserDao;
import com.yun.zhong.fu.usercenter.dto.message.UserAddBonusMsgDTO;
import com.yun.zhong.fu.usercenter.dto.user.UserLoginDTO;
import com.yun.zhong.fu.usercenter.entity.BonusEventLog;
import com.yun.zhong.fu.usercenter.entity.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.Date;
import java.util.List;

/**
 * @Author RenPu
 * @Date 2020/12/28 15:14
 * @Version 1.0
 * @Description:
 **/

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class UserService {

    private final UserDao userDao;

    private final BonusEventLogDao bonusEventLogDao;

    /**
     * 根据用户id值，进行获取用户的信息
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public User findById(Integer id) {
        User user = userDao.getById(id);
        return user;
    }



    /**
     * 1:先查询用户表是否存在此用户
     * 2:如果不存在，则直接进行插入
     * @param userLoginDTO
     * @param openId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public User login(UserLoginDTO userLoginDTO, String openId) {

        //1:先查询用户表是否存在此用户，如果存在此用户直接返回出去
        User user = this.userDao.selectOneData(User.builder()
                .wxId(openId)
                .build());
        //2：如果不存在，则直接进行插入
        if (user == null) {
            User userToSave = User.builder()
                    .avatarUrl(userLoginDTO.getAvatarUrl())
                    .bonus(50)
                    .wxId(openId)
                    .createTime(new Date())
                    .roles("user")
                    .wxNickname(userLoginDTO.getWxNickname())
                    .build();
            this.userDao.insert(userToSave);
            return userToSave;
        } else {
            return user;
        }
    }


    /**
     * 为用户加积分，同时保存到积分事件记录表中
     *
     * @param userAddBonusMsgDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public void addBonus(UserAddBonusMsgDTO userAddBonusMsgDTO) {

        //1:为用户加积分
        Integer userId = userAddBonusMsgDTO.getUserId();
        Integer bonus = userAddBonusMsgDTO.getBounus();
        User user = this.userDao.getById(userId);
        Integer addBouns = user.getBonus() + bonus;
        user.setBonus(addBouns);
        this.userDao.updateByPrimaryKeySelective(user);


        //2:把此次的积分累加的操作，记录到event_log
        this.bonusEventLogDao.insert(
                BonusEventLog
                        .builder()
                        .event(userAddBonusMsgDTO.getEvent())
                        .description(userAddBonusMsgDTO.getDescription())
                        .userId(userId)
                        .value(bonus)
                        .createTime(new Date())
                        .build());
        log.info("积分添加完毕.....");
    }


}
